<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/16
 * Time: 16:14
 */

namespace app\admin\controller;


use think\Db;

class Draw extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $draw_status = input('draw_status');
            $draw_condition = input('draw_condition');
            $keyword = input('keyword');
            
            $where = [];
            // 画师ID|昵称
            if(isset($keyword) && $keyword != ''){
                $where['draw_user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            // 抽奖状态
            if(isset($draw_status) && $draw_status != ''){
                $where['draw_status'] = $draw_status;
            }
            // 参加条件
            if(isset($draw_condition) && $draw_condition != ''){
                $where['draw_condition'] = $draw_condition;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('draw')
                ->join('up_u_user', 'user_id = draw_user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('draw')
                ->join('up_u_user', 'user_id = draw_user_id', 'LEFT')
                ->field('draw_id, draw_begin_date, draw_end_date, draw_condition, draw_status, 
                draw_time, user_username, draw_user_id, draw_explain')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('draw_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 奖品
     * @return mixed|\think\response\Json
     */
    public function drawInfo(){
        if(request()->isAjax ()){
            $where['di_draw_id'] = session('draw_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('draw_info')->where($where)->count();// 获取总条数
            $lists = Db::name('draw_info')
                ->field('di_name, di_image, di_num, di_prob, di_get_num')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('di_id asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $draw_id = input('draw_id');
        if(isset($draw_id) && $draw_id != ''){
            session('draw_id', $draw_id);
        }
        return $this->fetch('draw/drawinfo');
    }
    
    /**
     * 中奖列表
     * @return mixed|\think\response\Json
     */
    public function drawGet(){
        if(request()->isAjax ()){
            $keyword = input('keyword');
            $draw_info_name = input('draw_info_name'); // 奖品名称
            $md_draw_status = input('md_draw_status'); // 是否中奖
            
            $where['md_draw_id'] = session('draw_id');
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($draw_info_name) && $draw_info_name != ''){
                $where['di_name'] = ['like', '%' . $draw_info_name . '%'];
            }
            if(isset($md_draw_status) && $md_draw_status != ''){
                $where['md_draw_status'] = $md_draw_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('my_draw')
                ->join('up_u_user', 'user_id = md_user_id', 'LEFT')
                ->join('up_draw_info', 'di_id = md_draw_info_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('my_draw')
                ->join('up_u_user', 'user_id = md_user_id', 'LEFT')
                ->join('up_draw_info', 'di_id = md_draw_info_id', 'LEFT')
                ->field('user_id, user_username, di_name, md_draw_status, md_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('md_time asc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $draw_id = input('draw_id');
        if(isset($draw_id) && $draw_id != ''){
            session('draw_id', $draw_id);
        }
        return $this->fetch('draw/drawget');
    }
}